from pymilvus import connections
from .doc import (
    init_doc_collection,
    insert_doc_vectors,
    insert_doc_vector,
    search_doc_vectors,
)
from .image import (
    init_image_collection,
    insert_image_vector,
    insert_image_vectors,
    search_image_vectors,
)

# 定义一个全局变量，标识连接是否已经创建
_milvus_connnected = False


def ensure_milvus_connection(host="192.168.1.62", port="19530", db_name="search"):
    global _milvus_connnected
    if not _milvus_connnected:
        # 连接 到 milvus数据库
        connections.connect(host=host, port=port, db_name=db_name)
        # 初始化文档向量集合
        init_doc_collection()
        # 初始化图片的集合
        init_image_collection()
        _milvus_connnected = True


# 定义模块向外暴露的成员
__all__ = [
    ensure_milvus_connection,
    insert_doc_vectors,
    insert_doc_vector,
    search_doc_vectors,
    insert_image_vector,
    insert_image_vectors,
    search_image_vectors,
]
